﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.Domain.Model;

namespace SMS.Domain
{
    public class SmsService
    {
        public bool StoreCargo(Cargo cargo)
        {
            using (SmsContext context = new SmsContext())
            {
                if (context.Set<Cargo>()
                    .Where(p => p.Id == cargo.Id)
                    .Select(p => p.Id).SingleOrDefault() != default(Guid))
                {
                    context.Entry<Cargo>(cargo).State = System.Data.EntityState.Modified;
                }
                else
                {
                    context.Set<Cargo>().Add(cargo);
                }

                // 更新关键字库
                string[] keywords = cargo.Keywords.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in keywords)
                {
                    if (context.Set<Keywords>().Where(p => p.Name == item).SingleOrDefault() == null)
                    {
                        Keywords newKeywords = new Keywords();
                        newKeywords.Id = Guid.NewGuid();
                        newKeywords.Name = item;
                        context.Set<Keywords>().Add(newKeywords);
                    }
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public List<Cargo> FindCargos()
        {
            using (SmsContext context = new SmsContext())
            {
                return context.Set<Cargo>()
                    .ToList();
            }
        }

        public List<Cargo> FindCargos(Keywords keywords)
        {
            using (SmsContext context = new SmsContext())
            {
                return context.Set<Cargo>()
                    .Where(p=>p.Keywords.Contains(keywords.Name))
                    .ToList();
            }
        }

        public bool StoreCustomer(Customer customer)
        {
            using (SmsContext context = new SmsContext())
            {
                if (context.Set<Customer>()
                    .Where(p => p.Id == customer.Id)
                    .Select(p => p.Id).SingleOrDefault() != default(Guid))
                {
                    context.Entry<Customer>(customer).State = System.Data.EntityState.Modified;
                }
                else
                {
                    context.Set<Customer>().Add(customer);
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public List<Customer> FindCustomers()
        {
            using (SmsContext context = new SmsContext())
            {
                return context.Set<Customer>()
                    .ToList();
            }
        }

        public bool StoreCustomer(CustomerPrice price)
        {
            using (SmsContext context = new SmsContext())
            {
                if (context.Set<CustomerPrice>()
                    .Where(p => p.Id == price.Id)
                    .Select(p => p.Id).SingleOrDefault() != default(Guid))
                {
                    context.Entry<CustomerPrice>(price).State = System.Data.EntityState.Modified;
                }
                else
                {
                    context.Set<CustomerPrice>().Add(price);
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public List<CustomerPrice> FindPrices()
        {
            using (SmsContext context = new SmsContext())
            {
                return context.Set<CustomerPrice>()
                    .Include("Cargo")
                    .ToList();
            }
        }

        public List<Keywords> FindKeywords()
        {
            using (SmsContext context = new SmsContext())
            {
                return context.Set<Keywords>()
                    .OrderBy(p=>p.Name)
                    .ToList();
            }
        }

        public bool Install()
        {
            using (SmsContext context = new SmsContext())
            {
                if (context.Database.Exists())
                {
                    context.Database.Delete();
                }
                context.Database.CreateIfNotExists();
                return true;
            }
        }
    }
}
